﻿using SDL2;
using System;
using System.Collections.Generic;
using System.Text;

namespace BITUI.Element
{
    public class Button : IElement2D, IElementWithEvent
    {
        public int TileWidth
        {
            get
            {
                return this.stateNormal == null ? 1 : this.stateNormal.TileWidth;
            }

        }
        public int TileHeight
        {
            get
            {
                return this.stateNormal == null ? 1 : this.stateNormal.TileHeight;
            }
        }
        public IElement2D stateNormal;
        public IElement2D statePointOver;
        public IElement2D stateDisable;
        public IElement2D statePress;
        bool mouseOver = false;
        bool focus = false;
        public bool Enable = true;
        public IElement2DGroup Parent
        {
            get;
            private set;
        }

        public int TileXLocal { get; set; }
        public int TileYLocal { get; set; }

        public int TileX
        {
            get
            {
                if (this.Parent != null)
                    return this.Parent.TileX + this.TileXLocal;
                else
                    return this.TileXLocal;
            }
        }

        public int TileY
        {
            get
            {
                if (this.Parent != null)
                    return this.Parent.TileY + this.TileYLocal;
                else
                    return this.TileYLocal;
            }
        }
        public event Action OnClick;
        public void OnDraw(IWindowRender render)
        {
            IElement2D state = null;
            if (!Enable)
                state = stateDisable;

            //如果有焦点，就显示 stateFocus
            //如果有焦点并按住， 就显示 statePress
            if (mouseOver)
                state = statePointOver;


            if (focus)
                state = statePress;

            if (state == null)
                state = stateNormal;
            if (state != null)
            {
                state.SetParent(null);
                state.TileXLocal = this.TileX;
                state.TileYLocal = this.TileY;
                state.OnDraw(render);
            }
        }

        public void SetParent(IElement2DGroup parent)
        {
            this.Parent = parent;
        }

        bool IElementWithEvent.OnMouseDown(IWindowEvent win, byte button)
        {
            if (!this.mouseOver)
                return false;
            this.focus = true;
            win.RegFocus(this);
            return false;
        }

        bool IElementWithEvent.OnMouseMove(IWindowEvent win, int x, int y)
        {
            var x1 = this.TileX * 8;
            var y1 = this.TileY * 8;
            var x2 = (this.TileX + this.TileWidth) * 8;
            var y2 = (this.TileY + this.TileHeight) * 8;
            if (x1 <= x && x <= x2 && y1 < y && y < y2)
            {
                this.mouseOver = true;
                win.RegPointOver(this);
                return false;
            }
            //到我这就没了
            return true;
        }

        bool IElementWithEvent.OnMouseWheel(IWindowEvent win, uint dir, int x, int y)
        {//向上传
            if (this.Parent != null)
            {
                var b = Parent.OnMouseWheel(win, dir, x, y);
                if (!b) return false;
            }
            return true;
        }

        void IElementWithEvent.OnMouseUp(IWindowEvent win, byte button)
        {
            win.RegFocus(null);
            OnClick?.Invoke();

        }

        void IElementWithEvent.OnInput(IWindowEvent win, string text)
        {
        }

        void IElementWithEvent.OnMouseLeave(IWindowEvent win)
        {
            this.mouseOver = false;
        }

        void IElementWithEvent.OnLostFocus(IWindowEvent win)
        {
            this.focus = false;

        }
        public void OnKey(IWindowEvent win, SDL.SDL_Scancode key, bool press)
        {
        }
    }
}
